﻿Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions
Imports System.Web
Imports System.Net
Imports System.Globalization

Public Module ExtensionModule

    <Extension()> _
    Public Function DecodeValue(ByVal group As Capture) As String

        Dim text = group.Value.Replace(vbCrLf, "").Replace("<br />", vbCrLf)
        text = HttpUtility.HtmlDecode(Regex.Replace(text, "<.*?>", "", RegexOptions.Singleline))
        Return Regex.Replace(text, "(\r\n)+", vbCrLf).Trim

    End Function

    <Extension()> _
    Public Function DecodeValue(ByVal text As String) As String

        text = text.Replace(vbCrLf, "").Replace("<br />", vbCrLf)
        text = HttpUtility.HtmlDecode(Regex.Replace(text, "<.*?>", "", RegexOptions.Singleline))
        Return Regex.Replace(text, "(\r\n)+", vbCrLf).Trim

    End Function

    <Extension()> _
    Public Function MatchedText(ByVal inputText As String, ByVal pattern As String, ByVal label As String) As String
        Dim match = Regex.Match(inputText.Replace(vbLf, "").Replace(vbCr, "").Trim, pattern, RegexOptions.Singleline)
        If match.Success Then
            Return match.Groups(label).Value
        Else
            Return ""
        End If
    End Function

    <Extension()> _
    Public Function MatchedText(ByVal inputText As String, ByVal pattern As String) As String
        Dim match = Regex.Match(inputText.Replace(vbLf, "").Replace(vbCr, "").Trim, pattern, RegexOptions.Singleline)
        If match.Success Then
            Return match.Value
        Else
            Return ""
        End If
    End Function


#Region "Encrypt"

    <Extension()> _
       Public Function Encrypt(ByVal text As String) As String
        ' Ref: http://dobon.net/vb/dotnet/string/encryptstring.html
        If text = "" Then
            Return ""
        End If

        '文字列をバイト型配列にする
        Dim bytesIn As Byte() = System.Text.Encoding.UTF8.GetBytes(text)

        'DESCryptoServiceProviderオブジェクトの作成
        Dim des As New System.Security.Cryptography.DESCryptoServiceProvider

        '共有キーと初期化ベクタを決定
        'パスワードをバイト配列にする
        Dim bytesKey As Byte() = System.Text.Encoding.UTF8.GetBytes("みさお")
        '共有キーと初期化ベクタを設定
        des.Key = ResizeBytesArray(bytesKey, des.Key.Length)
        des.IV = ResizeBytesArray(bytesKey, des.IV.Length)

        '暗号化されたデータを書き出すためのMemoryStream
        Dim msOut As New System.IO.MemoryStream
        'DES暗号化オブジェクトの作成
        Dim desdecrypt As System.Security.Cryptography.ICryptoTransform = _
            des.CreateEncryptor()
        '書き込むためのCryptoStreamの作成
        Dim cryptStreem As New System.Security.Cryptography.CryptoStream( _
            msOut, desdecrypt, _
            System.Security.Cryptography.CryptoStreamMode.Write)
        '書き込む
        cryptStreem.Write(bytesIn, 0, bytesIn.Length)
        cryptStreem.FlushFinalBlock()
        '暗号化されたデータを取得
        Dim bytesOut As Byte() = msOut.ToArray()

        '閉じる
        cryptStreem.Close()
        msOut.Close()

        'Base64で文字列に変更して結果を返す
        Return System.Convert.ToBase64String(bytesOut)

    End Function

    <Extension()> _
    Public Function Decrypt(ByVal text As String) As String
        If text = "" Then
            Return ""
        End If

        'DESCryptoServiceProviderオブジェクトの作成
        Dim des As New System.Security.Cryptography.DESCryptoServiceProvider

        '共有キーと初期化ベクタを決定
        'パスワードをバイト配列にする
        Dim bytesKey As Byte() = System.Text.Encoding.UTF8.GetBytes("みさお")
        '共有キーと初期化ベクタを設定
        des.Key = ResizeBytesArray(bytesKey, des.Key.Length)
        des.IV = ResizeBytesArray(bytesKey, des.IV.Length)

        'Base64で文字列をバイト配列に戻す
        Dim bytesIn As Byte() = System.Convert.FromBase64String(text)
        '暗号化されたデータを読み込むためのMemoryStream
        Dim msIn As New System.IO.MemoryStream(bytesIn)
        'DES復号化オブジェクトの作成
        Dim desdecrypt As System.Security.Cryptography.ICryptoTransform = _
            des.CreateDecryptor()
        '読み込むためのCryptoStreamの作成
        Dim cryptStreem As New System.Security.Cryptography.CryptoStream( _
            msIn, desdecrypt, _
            System.Security.Cryptography.CryptoStreamMode.Read)

        '復号化されたデータを取得するためのStreamReader
        Dim srOut As New System.IO.StreamReader( _
            cryptStreem, System.Text.Encoding.UTF8)
        '復号化されたデータを取得する
        Dim result As String = srOut.ReadToEnd()

        '閉じる
        srOut.Close()
        cryptStreem.Close()
        msIn.Close()

        Return result
    End Function

    Private Function ResizeBytesArray(ByVal bytes() As Byte, _
                            ByVal newSize As Integer) As Byte()
        Dim newBytes(newSize - 1) As Byte
        If bytes.Length <= newSize Then
            Dim i As Integer
            For i = 0 To bytes.Length - 1
                newBytes(i) = bytes(i)
            Next i
        Else
            Dim pos As Integer = 0
            Dim i As Integer
            For i = 0 To bytes.Length - 1
                newBytes(pos) = newBytes(pos) Xor bytes(i)
                pos += 1
                If pos >= newBytes.Length Then
                    pos = 0
                End If
            Next i
        End If
        Return newBytes
    End Function

#End Region

End Module
